home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / inflow.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  13KB  |  505 lines

  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2.    Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25. #include "command.h"
  26. #include "signals.h"
  27. #include "terminal.h"
  28. #include "target.h"
  29.  
  30. #ifdef USG
  31. #include <sys/types.h>
  32. #endif
  33.  
  34. /* Some USG-esque systems (some of which are BSD-esque enough so that USG
  35.    is not defined) want this header, and it won't do any harm.  */
  36. #include <fcntl.h>
  37.  
  38. #include <sys/param.h>
  39. #include <sys/dir.h>
  40. #include <signal.h>
  41.  
  42. extern struct target_ops child_ops;
  43.  
  44. /* Nonzero if we are debugging an attached outside process
  45.    rather than an inferior.  */
  46.  
  47. int attach_flag;
  48.  
  49.  
  50. /* Record terminal status separately for debugger and inferior.  */
  51.  
  52. static TERMINAL sg_inferior;
  53. static TERMINAL sg_ours;
  54.  
  55. static int tflags_inferior;
  56. static int tflags_ours;
  57.  
  58. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  59. static struct tchars tc_inferior;
  60. static struct tchars tc_ours;
  61. #endif
  62.  
  63. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  64. static struct ltchars ltc_inferior;
  65. static struct ltchars ltc_ours;
  66. #endif
  67.  
  68. #ifdef TIOCLGET
  69. static int lmode_inferior;
  70. static int lmode_ours;
  71. #endif
  72.  
  73. #ifdef TIOCGPGRP
  74. # ifdef SHORT_PGRP
  75. static short pgrp_inferior;
  76. static short pgrp_ours;
  77. # else
  78. static int pgrp_inferior;
  79. static int pgrp_ours;
  80. # endif
  81. #else
  82. static void (*sigint_ours) ();
  83. static void (*sigquit_ours) ();
  84. #endif /* TIOCGPGRP */
  85.  
  86. /* Copy of inferior_io_terminal when inferior was last started.  */
  87. static char *inferior_thisrun_terminal;
  88.  
  89. static void terminal_ours_1 ();
  90.  
  91. /* Nonzero if our terminal settings are in effect.
  92.    Zero if the inferior's settings are in effect.  */
  93. static int terminal_is_ours;
  94.  
  95. /* Initialize the terminal settings we record for the inferior,
  96.    before we actually run the inferior.  */
  97.  
  98. void
  99. terminal_init_inferior ()
  100. {
  101.   sg_inferior = sg_ours;
  102.   tflags_inferior = tflags_ours;
  103.  
  104. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  105.   tc_inferior = tc_ours;
  106. #endif
  107.  
  108. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  109.   ltc_inferior = ltc_ours;
  110. #endif
  111.  
  112. #ifdef TIOCLGET
  113.   lmode_inferior = lmode_ours;
  114. #endif
  115.  
  116. #ifdef TIOCGPGRP
  117.   pgrp_inferior = inferior_pid;
  118. #endif /* TIOCGPGRP */
  119.  
  120.   terminal_is_ours = 1;
  121. }
  122.  
  123. /* Put the inferior's terminal settings into effect.
  124.    This is preparation for starting or resuming the inferior.  */
  125.  
  126. void
  127. terminal_inferior ()
  128. {
  129.   if (terminal_is_ours && inferior_thisrun_terminal == 0)
  130.     {
  131.       fcntl (0, F_SETFL, tflags_inferior);
  132.       fcntl (0, F_SETFL, tflags_inferior);
  133.       ioctl (0, TIOCSETN, &sg_inferior);
  134.  
  135. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  136.       ioctl (0, TIOCSETC, &tc_inferior);
  137. #endif
  138. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  139.       ioctl (0, TIOCSLTC, <c_inferior);
  140. #endif
  141. #ifdef TIOCLGET
  142.       ioctl (0, TIOCLSET, &lmode_inferior);
  143. #endif
  144.  
  145. #ifdef TIOCGPGRP
  146.       ioctl (0, TIOCSPGRP, &pgrp_inferior);
  147. #else
  148.       sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
  149.       sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
  150. #endif /* TIOCGPGRP */
  151.     }
  152.   terminal_is_ours = 0;
  153. }
  154.  
  155. /* Put some of our terminal settings into effect,
  156.    enough to get proper results from our output,
  157.    but do not change into or out of RAW mode
  158.    so that no input is discarded.
  159.  
  160.    After doing this, either terminal_ours or terminal_inferior
  161.    should be called to get back to a normal state of affairs.  */
  162.  
  163. void
  164. terminal_ours_for_output ()
  165. {
  166.   terminal_ours_1 (1);
  167. }
  168.  
  169. /* Put our terminal settings into effect.
  170.    First record the inferior's terminal settings
  171.    so they can be restored properly later.  */
  172.  
  173. void
  174. terminal_ours ()
  175. {
  176.   terminal_ours_1 (0);
  177. }
  178.  
  179. static void
  180. terminal_ours_1 (output_only)
  181.      int output_only;
  182. {
  183. #ifdef TIOCGPGRP
  184.   /* Ignore this signal since it will happen when we try to set the pgrp.  */
  185.   void (*osigttou) ();
  186. #endif /* TIOCGPGRP */
  187.  
  188.   /* The check for inferior_thisrun_terminal had been commented out
  189.      when the call to ioctl (TIOCNOTTY) was commented out.
  190.      Checking inferior_thisrun_terminal is necessary so that
  191.      if GDB is running in the background, it won't block trying
  192.      to do the ioctl()'s below.  */
  193.   if (inferior_thisrun_terminal != 0)
  194.     return;
  195.  
  196.   if (!terminal_is_ours)
  197.     {
  198.       terminal_is_ours = 1;
  199.  
  200. #ifdef TIOCGPGRP
  201.       osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
  202.  
  203.       ioctl (0, TIOCGPGRP, &pgrp_inferior);
  204.       ioctl (0, TIOCSPGRP, &pgrp_ours);
  205.  
  206.       signal (SIGTTOU, osigttou);
  207. #else
  208.       signal (SIGINT, sigint_ours);
  209.       signal (SIGQUIT, sigquit_ours);
  210. #endif /* TIOCGPGRP */
  211.  
  212.       tflags_inferior = fcntl (0, F_GETFL, 0);
  213.       ioctl (0, TIOCGETP, &sg_inferior);
  214.  
  215. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  216.       ioctl (0, TIOCGETC, &tc_inferior);
  217. #endif
  218. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  219.       ioctl (0, TIOCGLTC, <c_inferior);
  220. #endif
  221. #ifdef TIOCLGET
  222.       ioctl (0, TIOCLGET, &lmode_inferior);
  223. #endif
  224.     }
  225.  
  226. #ifdef HAVE_TERMIO
  227.   sg_ours.c_lflag |= ICANON;
  228.   if (output_only && !(sg_inferior.c_lflag & ICANON))
  229.     sg_ours.c_lflag &= ~ICANON;
  230. #else /* not HAVE_TERMIO */
  231.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  232.   if (output_only)
  233.     sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
  234. #endif /* not HAVE_TERMIO */
  235.  
  236.   fcntl (0, F_SETFL, tflags_ours);
  237.   fcntl (0, F_SETFL, tflags_ours);
  238.   ioctl (0, TIOCSETN, &sg_ours);
  239.  
  240. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  241.   ioctl (0, TIOCSETC, &tc_ours);
  242. #endif
  243. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  244.   ioctl (0, TIOCSLTC, <c_ours);
  245. #endif
  246. #ifdef TIOCLGET
  247.   ioctl (0, TIOCLSET, &lmode_ours);
  248. #endif
  249.  
  250. #ifdef HAVE_TERMIO
  251.   sg_ours.c_lflag |= ICANON;
  252. #else /* not HAVE_TERMIO */
  253.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  254. #endif /* not HAVE_TERMIO */
  255. }
  256.  
  257. /* ARGSUSED */
  258. void
  259. term_info (arg, from_tty)
  260.      char *arg;
  261.      int from_tty;
  262. {
  263.   target_terminal_info (arg, from_tty);
  264. }
  265.  
  266. /* ARGSUSED */
  267. void
  268. child_terminal_info (args, from_tty)
  269.      char *args;
  270.      int from_tty;
  271. {
  272.   register int i;
  273.  
  274.   printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
  275.  
  276. #ifdef HAVE_TERMIO
  277.  
  278.   printf_filtered ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
  279.       tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag);
  280.   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
  281.       sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
  282.   printf_filtered ("c_cc: ");
  283.   for (i = 0; (i < NCC); i += 1)
  284.     printf_filtered ("0x%x ", sg_inferior.c_cc[i]);
  285.   printf_filtered ("\n");
  286.  
  287. #else /* not HAVE_TERMIO */
  288.  
  289.   printf_filtered ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n",
  290.       tflags_inferior, sg_inferior.sg_flags, pgrp_inferior);
  291.  
  292. #endif /* not HAVE_TERMIO */
  293.  
  294. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  295.   printf_filtered ("tchars: ");
  296.   for (i = 0; i < (int)sizeof (struct tchars); i++)
  297.     printf_filtered ("0x%x ", ((char *)&tc_inferior)[i]);
  298.   printf_filtered ("\n");
  299. #endif
  300.  
  301. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  302.   printf_filtered ("ltchars: ");
  303.   for (i = 0; i < (int)sizeof (struct ltchars); i++)
  304.     printf_filtered ("0x%x ", ((char *)<c_inferior)[i]);
  305.   printf_filtered ("\n");
  306. #endif
  307.   
  308. #ifdef TIOCLGET
  309.   printf_filtered ("lmode:  0x%x\n", lmode_inferior);
  310. #endif
  311. }
  312.  
  313. /* NEW_TTY_PREFORK is called before forking a new child process,
  314.    so we can record the state of ttys in the child to be formed.
  315.    TTYNAME is null if we are to share the terminal with gdb;
  316.    or points to a string containing the name of the desired tty.
  317.  
  318.    NEW_TTY is called in new child processes under Unix, which will
  319.    become debugger target processes.  This actually switches to
  320.    the terminal specified in the NEW_TTY_PREFORK call.  */
  321.  
  322. new_tty_prefork (ttyname)
  323.      char *ttyname;
  324. {
  325.   /* Save the name for later, for determining whether we and the child
  326.      are sharing a tty.  */
  327.   inferior_thisrun_terminal = ttyname;
  328. }
  329.  
  330. void
  331. new_tty ()
  332. {
  333.   register int tty;
  334.  
  335.   if (inferior_thisrun_terminal == 0)
  336.     return;
  337.  
  338. #ifdef TIOCNOTTY
  339.   /* Disconnect the child process from our controlling terminal.  */
  340.   tty = open("/dev/tty", O_RDWR);
  341.   if (tty > 0)
  342.     {
  343.       ioctl(tty, TIOCNOTTY, 0);
  344.       close(tty);
  345.     }
  346. #endif
  347.  
  348.   /* Now open the specified new terminal.  */
  349.  
  350.   tty = open(inferior_thisrun_terminal, O_RDWR);
  351.   if (tty == -1)
  352.     {
  353.       print_sys_errmsg (inferior_thisrun_terminal, errno);
  354.       _exit(1);
  355.     }
  356.  
  357.   /* Avoid use of dup2; doesn't exist on all systems.  */
  358.   if (tty != 0)
  359.     { close (0); dup (tty); }
  360.   if (tty != 1)
  361.     { close (1); dup (tty); }
  362.   if (tty != 2)
  363.     { close (2); dup (tty); }
  364.   if (tty > 2)
  365.     close(tty);
  366. }
  367.  
  368. /* Kill the inferior process.  Make us have no inferior.  */
  369.  
  370. /* ARGSUSED */
  371. static void
  372. kill_command (arg, from_tty)
  373.      char *arg;
  374.      int from_tty;
  375. {
  376.   if (inferior_pid == 0)
  377.     error ("The program is not being run.");
  378.   if (!query ("Kill the inferior process? "))
  379.     error ("Not confirmed.");
  380.   target_kill (arg, from_tty);
  381.  
  382.   /* Killing off the inferior can leave us with a core file.  If so,
  383.      print the state we are left in.  */
  384.   if (target_has_stack) {
  385.     printf_filtered ("In %s,\n", current_target->to_longname);
  386.     if (selected_frame == NULL)
  387.       fputs_filtered ("No selected stack frame.\n", stdout);
  388.     else
  389.       print_stack_frame (selected_frame, selected_frame_level, 1);
  390.   }
  391. }
  392.  
  393. /* The inferior process has died.  Long live the inferior!  */
  394.  
  395. void
  396. generic_mourn_inferior ()
  397. {
  398.   inferior_pid = 0;
  399.   attach_flag = 0;
  400.   mark_breakpoints_out ();
  401.   registers_changed ();
  402.  
  403. #ifdef CLEAR_DEFERRED_STORES
  404.   /* Delete any pending stores to the inferior... */
  405.   CLEAR_DEFERRED_STORES;
  406. #endif
  407.  
  408.   reopen_exec_file ();
  409.   if (target_has_stack) {
  410.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  411.                       read_pc ()));
  412.     select_frame (get_current_frame (), 0);
  413.   } else {
  414.     set_current_frame (0);
  415.     select_frame ((FRAME) 0, -1);
  416.   }
  417.   /* It is confusing to the user for ignore counts to stick around
  418.      from previous runs of the inferior.  So clear them.  */
  419.   breakpoint_clear_ignore_counts ();
  420. }
  421.  
  422. void
  423. child_mourn_inferior ()
  424. {
  425.   unpush_target (&child_ops);
  426.   generic_mourn_inferior ();
  427. }
  428.  
  429. #if 0 
  430. /* This function is just for testing, and on some systems (Sony NewsOS
  431.    3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
  432.    (since on this system at least sys/time.h is not protected against
  433.    multiple inclusion).  */
  434. /* ARGSUSED */
  435. static void
  436. try_writing_regs_command (arg, from_tty)
  437.      char *arg;
  438.      int from_tty;
  439. {
  440.   register int i;
  441.   register int value;
  442.  
  443.   if (inferior_pid == 0)
  444.     error ("There is no inferior process now.");
  445.  
  446.   /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
  447.      kernel panic if we try to write past the end of the user area.
  448.      Presumably Sun will fix this bug (it has been reported), but it
  449.      is tacky to crash the system, so at least on SunOS4 we need to
  450.      stop writing when we hit the end of the user area.  */
  451.   for (i = 0; i < sizeof (struct user); i += 2)
  452.     {
  453.       QUIT;
  454.       errno = 0;
  455.       value = call_ptrace (3, inferior_pid, i, 0);
  456.       call_ptrace (6, inferior_pid, i, value);
  457.       if (errno == 0)
  458.     {
  459.       printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
  460.           i, value, value);
  461.     }
  462.       else if ((i & 0377) == 0)
  463.     printf (" Failed at 0x%x.\n", i);
  464.     }
  465. }
  466. #endif
  467.  
  468. void
  469. _initialize_inflow ()
  470. {
  471.   add_info ("terminal", term_info,
  472.        "Print inferior's saved terminal status.");
  473.  
  474. #if 0
  475.   add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
  476.        "Try writing all locations in inferior's system block.\n\
  477. Report which ones can be written.");
  478. #endif
  479.  
  480.   add_com ("kill", class_run, kill_command,
  481.        "Kill execution of program being debugged.");
  482.  
  483.   inferior_pid = 0;
  484.  
  485.   ioctl (0, TIOCGETP, &sg_ours);
  486.   tflags_ours = fcntl (0, F_GETFL, 0);
  487.  
  488. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  489.   ioctl (0, TIOCGETC, &tc_ours);
  490. #endif
  491. #if defined(TIOCGLTC) && !defined(TIOCGLTC_BROKEN)
  492.   ioctl (0, TIOCGLTC, <c_ours);
  493. #endif
  494. #ifdef TIOCLGET
  495.   ioctl (0, TIOCLGET, &lmode_ours);
  496. #endif
  497.  
  498. #ifdef TIOCGPGRP
  499.   ioctl (0, TIOCGPGRP, &pgrp_ours);
  500. #endif /* TIOCGPGRP */
  501.  
  502.   terminal_is_ours = 1;
  503. }
  504.  
  505.